﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections; 

using YT.DB;
using YT.Commons;

namespace YT.XH.MeniereDisease
{
    public partial class frmMDTherapeuticMethodMtn_Hurry : Form
    {
        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private const string mstrCaseType = "急性期";
        public frmMDTherapeuticMethodMtn_Hurry()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

        public frmMDTherapeuticMethodMtn_Hurry(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }

        private void btnContraindication_Click(object sender, EventArgs e)
        {
            frmMDMethodMtn_Hurry frmHurry = new frmMDMethodMtn_Hurry();
            frmHurry.ShowDialog(); 
        }

        private void SetDftCase(bool blnInit)
        {
            string strSQL = string.Empty;
            string strCaseName = string.Empty;
            string strContr = string.Empty;
            ArrayList arrName = new ArrayList();
            for (int i =1; i <= 17;i++ )
            {
                arrName.Add(i);
            }
            strSQL = "select distinct contraindicationname from mTherapeuticContraindication A " +
                    "inner join mContraindication B on a.contraindication =b.contraindicationType " +
                    "where sickrecid='" + mstrSickRecID + "' and B.Type='" + TitleType.MeniereDisease + "' and a.Type=B.Type";
            DataTable dtTable = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtTable.Rows)
            {
                strContr += ";" + CvtFunc.ToString(drRow["contraindicationname"]);
            }
            if (strContr.Length > 1) strContr = "此患者有: " + strContr.Substring(1, strContr.Length - 1) + " 禁忌症 ";
            int intCaseIndex = 0;
            strSQL = "select * from mTherapeuticContraindication A " +
                    "inner join mContraindication B on a.contraindication =b.contraindicationType " +
                    "where sickrecid='" + mstrSickRecID + "' and B.Type='" + TitleType.MeniereDisease + "' and a.Type=B.Type";
            DataTable dtCaseName = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtCaseName.Rows)
            {              
                string strNONCaseName = CvtFunc.ToString(drRow["TherapeuticMethod"]);
                {
                    if (strNONCaseName == "方案A1") { radA1.Enabled = false; arrName.Remove(1); }
                    if (strNONCaseName == "方案A2") {radA2.Enabled = false; arrName.Remove(2); }
                    if (strNONCaseName == "方案A3") {radA3.Enabled = false;arrName.Remove(3); }
                    if (strNONCaseName == "方案A4") {radA4.Enabled = false;arrName.Remove(4); }
                    if (strNONCaseName == "方案A5") {radA5.Enabled = false;arrName.Remove(5); }
                    if (strNONCaseName == "方案A6") {radA6.Enabled = false;arrName.Remove(6); }
                    if (strNONCaseName == "方案A7") {radA7.Enabled = false;arrName.Remove(7); }
                    if (strNONCaseName == "方案A8") {radA8.Enabled = false;arrName.Remove(8); }
                    if (strNONCaseName == "方案A9") {radA9.Enabled = false;arrName.Remove(9); }
                    if (strNONCaseName == "方案A10") {radA10.Enabled = false;arrName.Remove(10); }
                    if (strNONCaseName == "方案A11") {radA11.Enabled = false;arrName.Remove(11); }
                    if (strNONCaseName == "方案A12") {radA12.Enabled = false;arrName.Remove(12); }
                    if (strNONCaseName == "方案A13") { radA13.Enabled = false; arrName.Remove(13); }
                    if (strNONCaseName == "方案A14") { radA14.Enabled = false; arrName.Remove(14); }
                    if (strNONCaseName == "方案A15") { radA15.Enabled = false; arrName.Remove(15); }
                    if (strNONCaseName == "方案A16") { radA16.Enabled = false; arrName.Remove(16); }
                    if (strNONCaseName == "方案A17") { radA17.Enabled = false; arrName.Remove(17); }
                }
            }

            if (blnInit)
            {
                Random rad = new Random();
                int intRad = 1;
                int intMaxRecID = 13;

                while (true)
                {
                    intRad = rad.Next(1, intMaxRecID);
                    if (arrName.Contains(intRad)) break;
                }

                if (intRad == 1) radA1.Checked = true;
                if (intRad == 2) radA2.Checked = true;
                if (intRad == 3) radA3.Checked = true;
                if (intRad == 4) radA4.Checked = true;
                if (intRad == 5) radA5.Checked = true;
                if (intRad == 6) radA6.Checked = true;
                if (intRad == 7) radA7.Checked = true;
                if (intRad == 8) radA8.Checked = true;
                if (intRad == 9) radA9.Checked = true;
                if (intRad == 10) radA10.Checked = true;
                if (intRad == 11) radA11.Checked = true;
                if (intRad == 12) radA12.Checked = true;
                if (intRad == 13) radA13.Checked = true;
                if (intRad == 14) radA14.Checked = true;
                if (intRad == 15) radA15.Checked = true;
                if (intRad == 16) radA16.Checked = true;
                if (intRad == 17) radA17.Checked = true;
                lblDesc.Text = (strContr.Trim() == "" ? "" : strContr + "\r\n") + "系统推荐 " + GetOperationNameFromRad();
            }
            else
                lblDesc.Text = strContr;
            
        }

        private void FillCurOperation()
        {
            string strSQL = "select * from mSickPerson where sickrecid='" + mstrSickRecID + "' ";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow == null) return;

            if (CvtFunc.ToString(drRow["CurOperation"]) == "")
                SetDftCase(true);
            else
            {
                SetDftCase(false);
                GetRadFromOperationName(CvtFunc.ToString(drRow["CurOperation"]));
            }
        }

        private string GetOperationNameFromRad()
        {
            if (radA1.Checked) return "方案A1";
            if (radA2.Checked) return "方案A2";
            if (radA3.Checked) return "方案A3";
            if (radA4.Checked) return "方案A4";
            if (radA5.Checked) return "方案A5";
            if (radA6.Checked) return "方案A6";
            if (radA7.Checked) return "方案A7";
            if (radA8.Checked) return "方案A8";
            if (radA9.Checked) return "方案A9";
            if (radA10.Checked) return "方案A10";
            if (radA11.Checked) return "方案A11";
            if (radA12.Checked) return "方案A12";
            if (radA13.Checked) return "方案A13";
            if (radA14.Checked) return "方案A14";
            if (radA15.Checked) return "方案A15";
            if (radA16.Checked) return "方案A16";
            if (radA17.Checked) return "方案A17";
            return "";
        }

        private void GetRadFromOperationName(string strOperationName)
        {
            if (strOperationName == "方案A1") radA1.Checked = true;
            if (strOperationName == "方案A2") radA2.Checked = true;
            if (strOperationName == "方案A3") radA3.Checked = true;
            if (strOperationName == "方案A4") radA4.Checked = true;
            if (strOperationName == "方案A5") radA5.Checked = true;
            if (strOperationName == "方案A6") radA6.Checked = true;
            if (strOperationName == "方案A7") radA7.Checked = true;
            if (strOperationName == "方案A8") radA8.Checked = true;
            if (strOperationName == "方案A9") radA9.Checked = true;
            if (strOperationName == "方案A10") radA10.Checked = true;
            if (strOperationName == "方案A11") radA11.Checked = true;
            if (strOperationName == "方案A12") radA12.Checked = true;
            if (strOperationName == "方案A13") radA13.Checked = true;
            if (strOperationName == "方案A14") radA14.Checked = true;
            if (strOperationName == "方案A15") radA15.Checked = true;
            if (strOperationName == "方案A16") radA16.Checked = true;
            if (strOperationName == "方案A17") radA17.Checked = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string strSQL = string.Empty;
            string strOldCaseName = DataFunctions.GetFieldValue("mSickPerson", "CurOperation", "SickRecID='" + mstrSickRecID + "'");
            strSQL = "update mSickPerson set CurOperation='" + GetOperationNameFromRad() + "' where sickrecid='" + mstrSickRecID + "'";
            mdbcon.ExecuteSql(strSQL);
            string strTreatCaseName = GetOperationNameFromRad();

            if (strOldCaseName != strTreatCaseName)
            {
                int intPeriod = CvtFunc.ToInt(DataFunctions.GetFieldValue("mTreatHistory", "Period", "SickRecID='" + mstrSickRecID + "' and CaseType='"+ mstrCaseType +"'"));
                intPeriod++;

                strSQL = " INSERT INTO mTreatHistory (SickRecID, TreatCaseName,Period,CaseType  ) "
                        + " VALUES ( '" + mstrSickRecID + "', '" + strTreatCaseName + "', '" + intPeriod + "','"+ mstrCaseType +"' ) ";
                mdbcon.ExecuteSql(strSQL);
            }
            this.Close();
        }

        private void frmMDTherapeuticMethodMtn_Hurry_Load(object sender, EventArgs e)
        {
            this.Text = "治疗方案";
            FillCurOperation();
        }

        private void frmMDTherapeuticMethodMtn_Hurry_FormClosing(object sender, FormClosingEventArgs e)
        {
            mdbcon.CloseCon();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
